<!--
 * @Descripttion: (销售渠道佣金比例/lay_channel)
 * @Author: (admin)
 * @Date: (2024-10-22)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['laychannel:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['laychannel:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['laychannel:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!-- <el-table-column prop="id" label="Id" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('id')" /> -->
      <el-table-column prop="name" label="渠道名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('name')" />
      <el-table-column prop="code" label="渠道编码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('code')" />
      <el-table-column prop="contact" label="联系人" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('contact')" />
      <el-table-column prop="tel" label="联系电话" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('tel')" />
      <el-table-column prop="imageUrl" label="图标" align="center" v-if="columns.showColumn('imageUrl')">
        <template #default="scope">
          <ImagePreview :src="scope.row.imageUrl"></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column prop="typeId" label="类型" align="center" v-if="columns.showColumn('typeId')">
        <template #default="scope">
          <dict-tag :options="options.typeId" :value="scope.row.typeId" />
        </template>
      </el-table-column>
      <el-table-column prop="percent" :label="form.typeId == 1 ? '金额' : '比例'" align="center"
        v-if="columns.showColumn('percent')">
        <template #default="scope">
          {{ form.percent }}{{ form.typeId == 1 ? '元' : '%' }}
        </template>
      </el-table-column>
      <el-table-column prop="talAmount" label="总金额" align="center" v-if="columns.showColumn('talAmount')" />
      <el-table-column prop="hasAmount" label="可用金额" align="center" v-if="columns.showColumn('hasAmount')" />
      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('remark')" />
      <el-table-column prop="status" label="状态" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options="options.status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="primary" size="small" icon="view" title="详情" @click="handlePreview(scope.row)"></el-button>
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['laychannel:edit']"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['laychannel:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="8">
            <el-form-item label="渠道名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入渠道名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="渠道编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入渠道编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="佣金类型" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择类型">
                <el-option v-for="item in options.typeId" :key="item.dictValue" :label="item.dictLabel"
                  :value="parseInt(item.dictValue)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item :label="form.typeId == 1 ? '金额' : '比例'" prop="percent">
              <el-input type="number" v-model="form.percent" :min=0 placeholder="请输入金额">
                <template #suffix>{{ form.typeId == 1 ? "元" : "%" }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="返利结算" prop="days">
              <el-input type="number" v-model="form.days" :min=1 placeholder="请输入天">
                <template #suffix>天</template>
              </el-input>
              <span style="color: red">订单完结后{{ form.days }}天后返利</span>
            </el-form-item>

          </el-col>
          <el-col :lg="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status">
                <el-option v-for="item in options.status" :key="item.dictValue" :value="item.dictValue"
                  :label="item.dictLabel">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="24">
            <el-form-item label="渠道商LOGO" prop="imageUrl">
              <UploadImage v-model="form.imageUrl" :data="{ uploadType: 1 }" />
            </el-form-item>
          </el-col>
          <el-col :lg="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="laychannel">
import {
  listLayChannel,
  addLayChannel, delLayChannel,
  updateLayChannel, getLayChannel,
}
  from '@/api/business/laychannel.js'
import { min } from 'xe-utils';
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'CreateTime',
  sortType: 'desc',
  name: undefined
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'id', label: 'Id', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'name', label: '渠道名称', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'code', label: '渠道编码', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'contact', label: '联系人', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'tel', label: '联系电话', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'img', prop: 'imageUrl', label: '图标', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'typeId', label: '类型1==金额 2==比例' },
  { visible: true, align: 'center', type: '', prop: 'percent', label: '金额' },
  { visible: true, align: 'center', type: '', prop: 'talAmount', label: '总金额' },
  { visible: true, align: 'center', type: '', prop: 'hasAmount', label: '可用金额' },
  { visible: false, align: 'center', type: '', prop: 'remark', label: '备注', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'status', label: '状态', showOverflowTooltip: true },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
  "status",
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList() {
  loading.value = true
  listLayChannel(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    name: [{ required: true, message: "渠道名称不能为空", trigger: "blur" }],
    code: [{ required: true, message: "渠道编码不能为空", trigger: "blur" }],
    contact: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
    tel: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
    percent: [{ required: true, message: "金额或比例", trigger: "blur" }],
  },

  options: {
    status: [],
    // 类型1==金额 2==比例 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    typeId: [{ dictLabel: "比例", dictValue: "2" }, { dictLabel: "金额", dictValue: "1" }],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    name: null,
    code: null,
    contact: null,
    tel: null,
    imageUrl: null,
    typeId: null,
    percent: null,
    days: null,
    talAmount: null,
    hasAmount: null,
    remark: null,
    status: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
  };
  proxy.resetForm("formRef")
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
  const id = row.id
  getLayChannel(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data,
      }
    }
  })
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  form.value.typeId = 1
  form.value.status = '1'
  title.value = '添加销售渠道佣金比例'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getLayChannel(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改销售渠道佣金比例'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateLayChannel(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLayChannel(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLayChannel(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}




handleQuery()
</script>